/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function checkLogin(form) {
  var buf = '';
  var len = 0;
  var emailID=form.email

  if ((form.password.value == '') || (form.email.value == '')){

    buf = "Required fields: ";
    if (form.email.value == '')
      { buf = buf + " email, "; }
    if (form.password.value == '')
      { buf = buf + " password, "; }

    len = buf.length;
    buf = buf.substring(0,len-2);
    alert(buf);
    return false;
  }else{
    if (echeck(emailID.value)==false){
      emailID.value="";
      emailID.focus();
      alert("Invalid Format");
      return false
    }
  }
}

function checkContact(form) {
  var buf = '';
  var len = 0;
  var emailID=form.email

  if ((form.name.value == '') || (form.comment.value == '') || (form.email.value == '')){
    buf = "Required fields: ";
    if (form.name.value == '')
    { buf = buf + " full name, "; }
    if (form.comment.value == '')
    { buf = buf + " comment, "; }
    if (form.email.value == '')
    { buf = buf + " email, "; }

    len = buf.length;
    buf = buf.substring(0,len-2);
    alert(buf);
    return false;

  }else{
    if (echeck(emailID.value)==false){
    	emailID.value="";
    	emailID.focus();
    	alert("Email invalid");
    	return false
    }

    if (form.comment.value.length > 1000)
    {
    	alert('You only can write up to 1000 characters');
    	return false;
    }
  }
}

function checkRegister(form) {

  var buf = '';
  var len = 0;
  var emailID = form.user_email

  if ((form.user_name.value == '')
      || (form.user_last_name.value == '')
      || (form.user_login.value == '')
      || (form.user_password.value == '')
      || (form.user_email.value == '')
      || (form.user_mobile.value == '')){
    buf = "Required fields: ";
    if (form.user_name.value == '')
    { buf = buf + " your name, "; }
    if (form.user_last_name.value == '')
    { buf = buf + " your last name, "; }
    if (form.user_login.value == '')
    { buf = buf + " your login, "; }
    if (form.user_password.value == '')
    { buf = buf + " your password, "; }
    if (form.user_email.value == '')
    { buf = buf + " your email, "; }
    if (form.user_mobile.value == '')
    { buf = buf + " your mobile number, "; }

    len = buf.length;
    buf = buf.substring(0,len-2);
    alert(buf);
    return false;

  } else {
      if (echeck(emailID.value)==false)
      {
          emailID.value="";
          emailID.focus();
          alert("Invalid Format.");
          return false
      }
  }
  /*if (form.usuario_recibir_info.checked == false){
    alert("Para continuar debes marcar la casilla de aceptar los t" + String.fromCharCode(233) + "rminos");
    return false
  }*/

  if (form.user_password.value != form.user_password_repeat.value)
  {
    alert("The password doesn't match. Please try again");
    return false
  }

  if (form.user_email.value != form.user_email_repeat.value)
  {
    alert("The email doesn't match. Please try again");
    return false
  }

  return true;
}

function autoFillShipping(form) {

  var buf = '';
  var len = 0;
  var emailID=form.email

  if ((form.invoice_billing_name.value == '')
      || (form.invoice_billing_last_name.value == '')
      || (form.invoice_billing_company.value == '')
      || (form.invoice_billing_address.value == '')
      || (form.invoice_billing_city.value == '')
      || (form.invoice_billing_state.value == '')
      || (form.invoice_billing_zip.value == '')){
    buf = "First you have to fill: ";
    if (form.invoice_billing_name.value == '')
    { buf = buf + " billing name, "; }
    if (form.invoice_billing_last_name.value == '')
    { buf = buf + " billing last name, "; }
    if (form.invoice_billing_company.value == '')
    { buf = buf + " billing company, "; }
    if (form.invoice_billing_address.value == '')
    { buf = buf + " billing address, "; }
    if (form.invoice_billing_city.value == '')
    { buf = buf + " billing city, "; }
    if (form.invoice_billing_state.value == '')
    { buf = buf + " billing state, "; }
    if (form.invoice_billing_zip.value == '')
    { buf = buf + " billing zip, "; }



    len = buf.length;
    buf = buf.substring(0,len-2);
    alert(buf);
    return false;

  }

  form.invoice_shipping_name.value = form.invoice_billing_name.value;
  form.invoice_shipping_last_name.value =form.invoice_billing_last_name.value;
  form.invoice_shipping_company.value = form.invoice_billing_company.value;
  form.invoice_shipping_address.value = form.invoice_billing_address.value;
  form.invoice_shipping_city.value = form.invoice_billing_city.value;
  form.invoice_shipping_state.value = form.invoice_billing_state.value;
  form.invoice_shipping_zip.value = form.invoice_billing_zip.value;

  return true;
}

function checkCheckout(form) {

  var buf = '';
  var len = 0;
  var emailID=form.email

  if ((form.invoice_billing_name.value == '')
      || (form.invoice_billing_last_name.value == '')
      || (form.invoice_billing_company.value == '')
      || (form.invoice_billing_address.value == '')
      || (form.invoice_billing_city.value == '')
      || (form.invoice_billing_state.value == '')
      || (form.invoice_billing_zip.value == '')
      || (form.invoice_billing_phone.value == '')
      || (form.invoice_billing_email.value == '')
      || (form.invoice_shipping_name.value == '')
      || (form.invoice_shipping_last_name.value == '')
      || (form.invoice_shipping_company.value == '')
      || (form.invoice_shipping_address.value == '')
      || (form.invoice_shipping_city.value == '')
      || (form.invoice_shipping_state.value == '')
      || (form.invoice_shipping_zip.value == '')){
    buf = "Required fields: ";
    if (form.invoice_billing_name.value == '')
    { buf = buf + " billing name, "; }
    if (form.invoice_billing_last_name.value == '')
    { buf = buf + " billing last name, "; }
    if (form.invoice_billing_company.value == '')
    { buf = buf + " billing company, "; }
    if (form.invoice_billing_address.value == '')
    { buf = buf + " billing address, "; }
    if (form.invoice_billing_city.value == '')
    { buf = buf + " billing city, "; }
    if (form.invoice_billing_state.value == '')
    { buf = buf + " billing state, "; }
    if (form.invoice_billing_zip.value == '')
    { buf = buf + " billing zip, "; }
    if (form.invoice_billing_phone.value == '')
    { buf = buf + " billing phone, "; }
    if (form.invoice_billing_email.value == '')
    { buf = buf + " billing email, "; }
    if (form.invoice_shipping_name.value == '')
    { buf = buf + " shipping name, "; }
    if (form.invoice_shipping_last_name.value == '')
    { buf = buf + " shipping last name, "; }
    if (form.invoice_shipping_company.value == '')
    { buf = buf + " shipping company, "; }
    if (form.invoice_shipping_address.value == '')
    { buf = buf + " shipping address, "; }
    if (form.invoice_shipping_city.value == '')
    { buf = buf + " shipping city, "; }
    if (form.invoice_shipping_state.value == '')
    { buf = buf + " shipping state, "; }
    if (form.invoice_shipping_zip.value == '')
    { buf = buf + " shipping zip, "; }

    len = buf.length;
    buf = buf.substring(0,len-2);
    alert(buf);
    return false;

  }

  return true;
}

function checkCheckoutCredit(form) {

  var buf = '';
  var len = 0;

  if ((form.cvv2Number.value == '')
      || (form.expDateYear.value == '')
      || (form.expDateMonth.value == '')
      || (form.creditCardNumber.value == '')
      || (form.creditCardType.value == '')
      || (form.lastName.value == '')
      || (form.firstName.value == ''))
  {
    buf = "Required fields: ";
    if (form.firstName.value == '')
    { buf = buf + " credit card first name, "; }
    if (form.lastName.value == '')
    { buf = buf + " credit card last name, "; }
    if (form.creditCardType.value == '')
    { buf = buf + " credit card type, "; }
    if (form.creditCardNumber.value == '')
    { buf = buf + " credit card number, "; }
    if (form.expDateMonth.value == '' || form.expDateYear.value == '')
    { buf = buf + " credit card expiration date, "; }
    if (form.cvv2Number.value == '')
    { buf = buf + " credit card security code, "; }

    len = buf.length;
    buf = buf.substring(0,len-2);
    alert(buf);
    return false;

  }

  return true;
}

function checkForgotPassword(form) {
  var buf = '';
  var len = 0;
  var emailID=form.email


  if (form.email.value == '')
  {
    buf = "Required fields: ";
    if (form.email.value == '')
    { buf = buf + " email, "; }

    len = buf.length;
    buf = buf.substring(0,len-2);
    alert(buf);
    return false;
  }else{
    if (echeck(emailID.value)==false){
      emailID.value="";
      emailID.focus();
      alert("Invalid Format.");
      return false
    }
  }
  return true;
}

function echeck(str)
{
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)

  if (str.indexOf(at)==-1)
  {
	return false
  }
  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
  {
	return false
  }
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	return false
  }
  if (str.indexOf(at,(lat+1))!=-1)
  {
	return false
  }

  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
  {
	return false
  }
  if (str.indexOf(dot,(lat+2))==-1)
  {
	return false
  }
  if (str.indexOf(" ")!=-1)
  {
	return false
  }
  return true
}
